﻿using MediatR;
using Medical.Api.Read.Application.Command.DepartmentManagement.Adopted;
using Medical.Api.Read.Application.Command.DepartmentManagement.Recycling;
using Medical.Api.Read.Application.Command.DepartmentManagement.Sign;
using Medical.Api.Read.Application.Command.DepartmentManagement.Use;
using Medical.Api.Read.Dto.DepartmentManagement.Adopted;
using Medical.Api.Read.Dto.DepartmentManagement.Sign;
using Medical.Domain.DepartmentInfo;
using Medical.ErrorCode;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

namespace Medical.Api.Read.Controllers
{
    /// <summary>
    /// 科室管理
    /// </summary>
    [Route("api/[controller]")]
    [ApiController]
    public class DepartmentManagementController : ControllerBase
    {
        private readonly IMediator mediator;
        private readonly ILogger<DepartmentManagementController> logger;
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="mediator"></param>
        /// <param name="logger"></param>
        public DepartmentManagementController(IMediator mediator, ILogger<DepartmentManagementController> logger)
        {
            this.mediator = mediator;
            this.logger = logger;
        }
        /// <summary>
        /// 领用申请列表
        /// </summary>
        /// <param name="cmd">查询参数</param>
        /// <returns>领用表列表结果</returns>
        [HttpGet("adopted")]
        public async Task<ApiResult<List<TouseTable>>> GetAdoptedCommandAsync([FromQuery] GetAdoptedCommands cmd)
        {
            try
            {
                logger.LogInformation("获取领用申请列表请求");
                var result = await mediator.Send(cmd);
                return result;
            }
            catch (Exception ex)
            {
                logger.LogError(ex, "获取领用申请列表时发生错误");
                return new ApiResult<List<TouseTable>>
                {
                    Code = ApiEnums.Error,
                    Data = new List<TouseTable>(),
                    Msg = $"获取领用申请列表失败: {ex.Message}"
                };
            }
        }

        /// <summary>
        /// 领用申请详情反填
        /// </summary>
        /// <param name="touseId">领用申请ID</param>
        /// <returns>领用申请和物品详情</returns>
        [HttpGet("adoptedId")]
        public async Task<ApiResult<AdoptedDetailDto>> GetAdoptedDetailAsync(int touseId)
        {
            try
            {
                logger.LogInformation($"获取领用申请详情请求，ID：{touseId}");
                var cmd = new GetAdoptedGetModelCommands { touseId = touseId };
                var result = await mediator.Send(cmd);
                return result;
            }
            catch (Exception ex)
            {
                logger.LogError(ex, $"获取领用申请详情时发生错误，ID：{touseId}");
                return new ApiResult<AdoptedDetailDto>
                {
                    Code = ApiEnums.Error,
                    Data = null,
                    Msg = $"获取领用申请详情失败: {ex.Message}"
                };
            }
        }
        
        /// <summary>
        /// 签收列表
        /// </summary>
        /// <param name="cmd">查询参数，包括发放表ID</param>
        /// <returns>签收表列表结果</returns>
        [HttpGet("sign")]
        public async Task<ApiResult<List<SignDto>>> GetSignCommandAsync([FromQuery] GetSignCommands cmd)
        {
            try
            {
                logger.LogInformation($"获取签收列表请求");
                var result = await mediator.Send(cmd);
                return result;
            }
            catch (Exception ex)
            {
                logger.LogError(ex, "获取签收列表时发生错误");
                return new ApiResult<List<SignDto>>
                {
                    Code = ApiEnums.Error,
                    Data = new List<SignDto>(),
                    Msg = $"获取签收列表失败: {ex.Message}"
                };
            }
        }
        
        /// <summary>
        /// 使用登记列表
        /// </summary>
        /// <param name="cmd">查询参数</param>
        /// <returns>使用表列表结果</returns>
        [HttpGet("use")]
        public async Task<ApiResult<ApiPageIng<UseTable>>> GetUseCommandAsync([FromQuery] GetUseCommands cmd)
        {
            try
            {
                logger.LogInformation("获取使用登记列表请求");
                var result = await mediator.Send(cmd);
                return result;
            }
            catch (Exception ex)
            {
                logger.LogError(ex, "获取使用登记列表时发生错误");
                return new ApiResult<ApiPageIng<UseTable>>
                {
                    Code = ApiEnums.Error,
                    Data = new ApiPageIng<UseTable>(),
                    Msg = $"获取使用登记列表失败: {ex.Message}"
                };
            }
        }

        /// <summary>
        /// 回收申请列表
        /// </summary>
        /// <param name="cmd">查询参数</param>
        /// <returns>回收申请表分页结果</returns>
        [HttpGet("recycling")]
        public async Task<ApiResult<ApiPageIng<RecyclingTable>>> GetRecyclingCommandAsync([FromQuery] GetRecyclingCommands cmd)
        {
            try
            {
                logger.LogInformation("获取回收申请列表请求");
                var result = await mediator.Send(cmd);
                return result;
            }
            catch (Exception ex)
            {
                logger.LogError(ex, "获取回收申请列表时发生错误");
                return new ApiResult<ApiPageIng<RecyclingTable>>
                {
                    Code = ApiEnums.Error,
                    Data = new ApiPageIng<RecyclingTable>(),
                    Msg = $"获取回收申请列表失败: {ex.Message}"
                };
            }
        }
    }
}
